module tic_tac_toe_game(
    input clock,                     // 时钟
    input reset,                     // 复位
    input play,                      // 玩家输入信号 
    input pc,                        // 电脑输入信号
    input [3:0] computer_position,   // 电脑位置
    input [3:0] player_position,     // player position to play
    output wire [1:0] pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, 
                                   // led位置
                                   // 01: 玩家
                                   // 10: 电脑 
    output wire [1:0] who,            // 赢家是谁
                                   // 01: 玩家
                                   // 10: 电脑 
    output wire even_score_flag       // 平局信号
);

    wire [15:0] PC_en;               // Computer enable signals 
    wire [15:0] PL_en;               // Player enable signals 
    wire illegal_move;               // disable writing when an illegal move is detected 
    wire winner;                        // win signal 
    // wire computer_play;              // computer enabling signal 
    // wire player_play;                // player enabling signal 
    wire no_space;                   // no space signal 



 position_decoder   u_position_decoder1(
    .in        (player_position), 
    // .enable        (player_play), 
    .out_en        (PL_en)
    );

 position_decoder   u_position_decoder2(
    .in        (computer_position), 
    // .enable        (computer_play), 
    .out_en        (PC_en)
    );
    // 棋盘
qipan_state   u_qipan_state(
    . clock      (clock), // 时钟
    . reset      (reset), // 复位 
    . illegal_move      (illegal_move), //  检测到非法输入时，禁止写入
    . PC_en      (PC_en[8:0]), //  电脑有效信号
    . PL_en      (PL_en[8:0]), //   玩家有效信号
    . pos1       (pos1)             ,
    . pos2       (pos2)             ,
    . pos3       (pos3)             ,
    . pos4       (pos4)             ,
    . pos5       (pos5)             ,
    . pos6       (pos6)             ,
    . pos7       (pos7)             ,
    . pos8       (pos8)             ,
    . pos9       (pos9)             //  储存位置
      );


    // 赢家判断
     winner_detector u_winner_detector(
     . pos1(pos1),
     . pos2(pos2),
     . pos3(pos3),
     . pos4(pos4),
     . pos5(pos5),
     . pos6(pos6),
     . pos7(pos7),
     . pos8(pos8),
     . pos9(pos9), 
     . winner(winner), 
     . who(who)

);




    // 非法放置棋子
    error_move u_error_move(
    . pos1   (pos1),
    . pos2   (pos2),
    . pos3   (pos3),
    . pos4   (pos4),
    . pos5   (pos5),
    . pos6   (pos6),
    . pos7   (pos7),
    . pos8   (pos8),
    . pos9   (pos9), 
    . PC_en   (PC_en[8:0]),
    . PL_en   (PC_en[8:0]), 
    . illegal_move   (illegal_move)
   );


    // 棋盘无空间判断
    nospace_detector u_nospace_detector(

    . pos1   (pos1),
    . pos2   (pos2),
    . pos3   (pos3),
    . pos4   (pos4),
    . pos5   (pos5),
    . pos6   (pos6),
    . pos7   (pos7),
    . pos8   (pos8),
    . pos9   (pos9), 
    . no_space (no_space)
    );


    // FSM Controller
fsm_controller u_fsm_controller(
     . clock        (clock),// 时钟
     . reset        (reset),// 复位 
     . play        (play), // 玩家 
     . pc           (pc),// 电脑
     . illegal_move        (illegal_move),// 非法移动信号 
     . no_space        (no_space), // 没位置信号 
     . win        (winner), // 胜利信号 
    //  . computer_play        (computer_play), // 电脑使能输出 
    //  . player_play        (player_play),   // 玩家使能输出 
     . even_score_flag        (even_score_flag) // 平局
     );
endmodule